/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.una.scece.servicios;

import com.una.scece.control.controlGestor;
import com.una.scece.clases.Correo;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Erick Pérez Ibarra
 */
public class servicioUtiles extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String accion = request.getParameter("accion");
        try {
            if (accion.equalsIgnoreCase("agregarId")) {
                this.agregarIdentificacion(request, response);
            } else if (accion.equalsIgnoreCase("agregarIdFunc")) {
                this.agregarIdentificacionFuncionarios(request, response);
            } else if (accion.equalsIgnoreCase("agregarId2")) {
                //this.agregarIdentificacion2(request, response);
            } else if(accion.equalsIgnoreCase("agregarCursos")){
                this.cargarCursos(request, response);
            } else if(accion.equalsIgnoreCase("agregarCursosEst")){
                this.cargarCursosEstudiante(request, response);
            } else if(accion.equalsIgnoreCase("enviarCorreoMorosos")){
                this.enviarCorreoMosorosos(request, response);
            }
        } finally {
            out.close();
        }
    }

    protected void agregarIdentificacion(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String datos = "";
        try {
            ArrayList<String> lista = controlGestor.verIdentificacionEstudiantes();
            for (int i = 0; i < lista.size(); i++) {
                datos += lista.get(i) + "|";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        out.println(datos);
    }

    protected void agregarIdentificacionFuncionarios(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String datos = "";
        try {
            ArrayList<String> lista = controlGestor.verIdentificacionFuncionarios();
            for (int i = 0; i < lista.size(); i++) {
                datos += lista.get(i) + "|";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        out.println(datos);
    }

    protected void enviarCorreoMosorosos(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("application/text");
        PrintWriter out = response.getWriter();
        try {
            Integer anio = Integer.parseInt(request.getParameter("anio"));
            String trimestre = request.getParameter("trimestre");
            ArrayList<String> lista = controlGestor.correoMorosos(anio, trimestre);
            String observaciones = request.getParameter("observaciones");
            Correo correo = new Correo();
            if (lista.size() > 0) {
                for (int i = 0; i < lista.size(); i++) {
                    correo.enviarCorreo(lista.get(i), "Estudiantes Pendientes de Pago", observaciones);
                }
            }
            response.setStatus(HttpServletResponse.SC_OK);
            out.println("Notificaciones enviadas correctamente");
        } catch (Exception e) {
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            out.println("Notificaciones no enviadas correctamente");
            e.printStackTrace();
        }
        finally{
            out.close();
        }
    }

    protected void cargarCursos(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String datos = "";
        try {
            int anio = Integer.parseInt(request.getParameter("anio"));
            String trimestre = request.getParameter("trimestre");
            ArrayList<String> lista = controlGestor.verIdentificacionCursos(anio, trimestre);
            for (int i = 0; i < lista.size(); i++) {
                datos += lista.get(i) + "|";
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            out.println(datos);
            out.close();
        }
    }

        protected void cargarCursosEstudiante(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            int anio = Integer.parseInt(request.getParameter("anio"));
            String trimestre = request.getParameter("trimestre");
            String identificacion = request.getParameter("identificacion");
            String lista = controlGestor.verCursosEstudiante2(identificacion, anio, trimestre);
            /*for (int i = 0; i < lista.size(); i++) {
                datos += lista.get(i) + "|";
            }*/
            out.println(lista);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            out.close();
        }
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
